//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Research.DataLayer;

namespace Microsoft.Research.DataLayer
{
    public partial class ConnMgrDlg_OPC : ConnectionManagerDialog_Base
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public ConnMgrDlg_OPC()
        {
            InitializeComponent();

            Presets.OnLoadPreset += new ConnMgrDlg_Presets.PresetHandler(presets_OnLoadPreset);
        }

        /// <summary>
        /// Get the text representing the file location.
        /// </summary>
        public override string DisplayStatus
        {
            get
            {
                return string.Format("Local Store: {0}", this.filePath.Text);
            }
        }

        /// <summary>
        /// Test connection and give user feedback
        /// </summary>
        public override void TestConnection()
        {
            try
            {
                OPCConnection testConn = OPCConnection.Create(this.filePath.Text);
                TestConnection(testConn);
            }
            catch (BackendStorageException ex)
            {
                MessageBox.Show(string.Format("Cannot connect to storage services.\n\nError: {0}",
                    ex.Message), "ERROR", MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
            }
        }


        private void presets_OnLoadPreset(IConnMgrInfoParams inPreset)
        {
            OPCConnectionParameters preset = inPreset as OPCConnectionParameters;
            if (preset != null)
            {
                string path;
                preset.Load(out path);
                this.filePath.Text = path;
            }
            else
            {
                this.filePath.Text = "";
            }

            FireOnPropertyChange(false);
        }

        private void savePreset_Click(object sender, EventArgs e)
        {
            SaveItem();
        }

        /// <summary>
        /// Save the opc file.
        /// </summary>
        protected override void SaveItem()
        {
            OPCConnectionParameters preset = Presets.CurrentPreset as OPCConnectionParameters;
            preset.Update(preset.Name, this.filePath.Text);
            base.SaveItem();
        }

        private void path_TextChanged_1(object sender, EventArgs e)
        {
            FireOnPropertyChange(true);
        }

        /// <summary>
        /// Return the connection object.
        /// </summary>
        public override Connection Connection
        {
            get
            {
                return OPCConnection.Create(this.filePath.Text);
            }
        }

        private void opcFileBrowse_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                filePath.Text = openFileDialog1.FileName;
            }
        }

        /// <summary>
        /// Create a new preset
        /// </summary>
        /// <param name="name">Name of the preset</param>
        /// <param name="newPreset">Paremters for creating the preset</param>
        /// <param name="provider">Name of the provider</param>
        protected override void CreateNewPreset(string name, out IConnMgrInfoParams newPreset, out ProviderPkgInfo provider)
        {
            newPreset = OPCConnectionParameters.CreateConnection(name, filePath.Text);
            provider = this.Provider;
        }

        /// <summary>
        /// Method to make sure if the connection provides testing
        /// feature.
        /// </summary>
        /// <returns>True if it does</returns>
        public override bool CanTestConnection()
        {
            return (filePath.Text.Length > 0);
        }
    }
}
